﻿using ImageMagick;
using MediaInfoDotNet;
using Newtonsoft.Json;
using PlayListSet.Comm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace PlayListSet.Entity
{

    public class PlayInfo: PlayInfoBase
    {
       
     
        public PlayInfo()
        {
            PlayInfoUserCommand = new UserCommand();
            PlayInfoUserCommand.ExecuteEvent += UserCommand_ExecuteEvent;
        }


       
        private Size channelInfo = new Size();
        [JsonIgnore]
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public Size ChannelInfo
        {
            set
            {
                channelInfo = value;
                PropertyChangedEvent("ChannelInfo");
                PropertyChangedEvent("ChannelInfoStr");
            }
            get { return channelInfo; }
        }
        [JsonIgnore]
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public string ChannelInfoStr
        {
            get { return Channel + "  分辨率：" + channelInfo.Width + "*" + channelInfo.Height; }
        }
       

      
        private double startPlayTime = 0;
        public double StartPlayTime
        {
            set { 
                startPlayTime = value; 
                PropertyChangedEvent("StartPlayTime");
            }
            get { return startPlayTime; }
        }
      
        private int sourceType = 0;
        /// <summary>
        /// 0 视频 1 图片 2 音频 3 ndi 4 spout
        /// </summary>
        public int SourceType
        {
            set { sourceType = value; PropertyChangedEvent("SourceType"); }
            get { return sourceType; }
        }
        private string sourceUrl=string.Empty;
        public string SourceUrl
        {
            set { sourceUrl = value; PropertyChangedEvent("SourceUrl"); PropertyChangedEvent("SourceFileName"); }
            get { return sourceUrl; }
        }
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public string SourceFileName
        {
            get {
                try
                {
                    return System.IO.Path.GetFileName(SourceUrl);

                }
                catch (Exception)
                {
                    return SourceUrl;
                }
            }
        }
        private int showType;
        public int ShowType
        {
            set { showType = value; PropertyChangedEvent("ShowType"); }
            get { return showType; }
        }
        private int left;
        public int Left
        {
            set { left = value; PropertyChangedEvent("Left"); }
            get { return left; }
        }
        private int top;
        public int Top
        {
            set { top = value; PropertyChangedEvent("Top"); }
            get { return top; }
        }
        private int width;
        public int Width
        {
            set { width = value; PropertyChangedEvent("Width"); }
            get { return width; }
        }
        private int height;
        public int Height
        {
            set { height = value; PropertyChangedEvent("Height"); }
            get { return height; }
        }

        private bool fileX;
        public bool FileX
        {
            set { fileX = value; PropertyChangedEvent("FileX"); }
            get { return fileX; }
        }
        private bool fileY;
        public bool FileY
        {
            set { fileY = value; PropertyChangedEvent("FileY"); }
            get { return fileY; }
        }
        private int flop;
        public int Flop
        {
            set { flop = value; PropertyChangedEvent("Flop"); }
            get { return flop; }
        }
        private bool mute = false;
        public bool Mute
        {
            set { mute = value; PropertyChangedEvent("Mute"); }
            get { return mute; }
        }
        private int volume=100;
        public int Volume
        {
            set { volume = value; PropertyChangedEvent("Volume"); }
            get { return volume; }
        }

        private double soureceTime;
        public double SoureceTime
        {
            set { soureceTime = value; PropertyChangedEvent("SoureceTime"); }
            get { return soureceTime; }
        }
        private double sourceWidth;
        public double SourceWidth
        {
            set { sourceWidth = value; PropertyChangedEvent("SourceWidth"); }
            get { return sourceWidth; }
        }
        private double sourceHeight;
        public double SourceHeight
        {
            set { sourceHeight = value; PropertyChangedEvent("SourceHeight"); }
            get { return sourceHeight; }
        }




        #region UI Model

        [JsonIgnore]
        [SqlSugar.SugarColumn(IsIgnore =true)]
        public UserCommand PlayInfoUserCommand { set; get; }
        private void UserCommand_ExecuteEvent(object parameter)
        {
            string p = parameter as string;
            if (p == "浏览")
            {
                Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
                if (sourceType == 0)
                {
                    openFileDialog.Filter = "视频|*.mov;*.mp4;*.avi;*.flv;*.wmv;*.mkv;";
                }
                else if (sourceType == 1)
                {
                    openFileDialog.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
                }
                else if (sourceType == 2)
                {
                    openFileDialog.Filter = "音频|*.mp3;*.wav;";
                }
                if (openFileDialog.ShowDialog() == true)
                {
                    SourceUrl = openFileDialog.FileName;
                    UpdateSourceInfo();
                }
            }
            else if (p == "预览")
            {
                Process.Start(SourceUrl);
            }
            else if (p == "刷新")
            {
                UpdateSourceInfo();
            }
            else if (p == "使用资源时长")
            {
                EndTimeCode = StartTimeCode + SoureceTime;
            }
        }
        private void UpdateSourceInfo()
        {
            try
            {
                if (sourceType == 0)
                {
                    MediaFile aviFile = new MediaFile(SourceUrl);
                    SoureceTime = aviFile.General.duration / 1000;
                    if (aviFile.Video.Count > 0)
                    {
                        SourceWidth = aviFile.Video[0].width;
                        SourceHeight = aviFile.Video[0].height;
                    }

                }
                else if (sourceType == 1)
                {
                    SoureceTime = 10;
                    MagickImageInfo image = new MagickImageInfo(SourceUrl);
                    SourceWidth = image.Width;//宽
                    SourceHeight = image.Height;//高

                }
                else if (sourceType == 2)
                {
                    SoureceTime = 10;
                    NAudio.Wave.AudioFileReader audioFileReader = new NAudio.Wave.AudioFileReader(SourceUrl);
                    SoureceTime = audioFileReader.TotalTime.TotalSeconds;



                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("读取信息失败：" + ex.Message);
            }
        }

        private LocalAudioPlayState localAudioPlayState;
        public LocalAudioPlayState LocalAudioPlayState
        {
            set { localAudioPlayState = value; PropertyChangedEvent("LocalAudioPlayState"); }
            get { return localAudioPlayState; }
        }
        private double localAudioSourceTime;
        public double LocalAudioSourceTime
        {
            set { localAudioSourceTime = value; PropertyChangedEvent("LocalAudioSourceTime"); }
            get { return localAudioSourceTime; }
        }
        #endregion


        public static void Save(List<PlayInfo> infos)
        {
            //var json = Newtonsoft.Json.JsonConvert.SerializeObject(infos, Formatting.Indented);
            //System.IO.File.WriteAllText(
            //    System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "cfg", "PlayInfos.json"),
            //    json, Encoding.UTF8);
            var db = AppHelp.GetAppSqlSugarClient();
            db.Deleteable<PlayInfo>().ExecuteCommand();
             db.Insertable<PlayInfo>(infos).ExecuteCommand();
        }
        public static List<PlayInfo> Read()
        {
            //var text = System.IO.File.ReadAllText(
            //     System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "cfg", "PlayInfos.json"), Encoding.UTF8);
            //return Newtonsoft.Json.JsonConvert.DeserializeObject<List<PlayInfo>>(text);
            var cfg = AppHelp.GetAppSqlSugarClient().Queryable<PlayInfo>().ToList();
            if (cfg == null)
                return new List<PlayInfo>();
            return cfg;
        }
        public static string GetTabLine()
        {
            return $"ID\tChannel\tName\tStartTimeCode\tEndTimeCode\tStartPlayTime\tSourceType\tSourceUrl\tFileName\tShowType\tLeft\tTop\tWidth\tHeight\tFileX\tFileY\tFlop\tSoureceTime\tSourceWidth\tSourceHeight\tMute\tVolume";

        }
        public string GetLine()
        {
            var fileName = System.IO.Path.GetFileName(SourceUrl);
            return $"{ID}\t{Channel}\t{Name}\t{StartTimeCode}\t{EndTimeCode}\t{StartPlayTime}\t{SourceType}\t{SourceUrl}\t{fileName}\t{ShowType}\t{Left}\t{Top}\t{Width}\t{Height}\t{(FileX ? 1 : 0)}\t{(FileY ? 1 : 0)}\t{Flop}\t{SoureceTime}\t{SourceWidth}\t{SourceHeight}\t{Mute}\t{Volume}";
        }
    }
}
